﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Hosting;
using System.Web.Mvc;

//using CreativeMinds.MVC.Common.Binders;

using MVCThemes;
using MVCThemes.Interfaces;

using mvcForum.Web.Filters;
using mvcForum.Core.Interfaces.DependencyManagement;
using mvcForum.Web.Binders;

namespace mvcForum.Web {

	public class ForumConfigurator : IDependencyBuilder {

		public void Configure(IDependencyContainer container) {
			// Register the virtual path provider for the embedded views.
			//HostingEnvironment.RegisterVirtualPathProvider(new CompiledVirtualPathProvider());

			// Register embedded views for this assembly!
			//ApplicationPartRegistry.Register(this.GetType().Assembly);

			// We'll need to replace the RazorEngine with the MVC Themes engine!
			IEnumerable<IViewEngine> razor = ViewEngines.Engines.Where<IViewEngine>(x => x is RazorViewEngine);
			// Did we find the razor engine?
			if (razor.Any()) {
				// Yes, let's remove it!
				ViewEngines.Engines.Remove(razor.First());
			}
			// Add the theme engine!
			ViewEngines.Engines.Add(new ThemeViewEngine());

			// We need to be able to handle Enum Flags, so let's add a new default binder if it hasn't already been done.
			if (!(ModelBinders.Binders.DefaultBinder is FlagEnumerationModelBinder)) {
				ModelBinders.Binders.DefaultBinder = new FlagEnumerationModelBinder();
			}

			// Have the CultureInfoFilterAttribute already been added?
			//if (!GlobalFilters.Filters.Where<Filter>(x => x.Instance is CultureInfoFilterAttribute).Any()) {
			//    // Nope, let's add it then!
			//    GlobalFilters.Filters.Add(new CultureInfoFilterAttribute(), 1);
			//}
			// Have the LastVisitedFilterAttribute already been added?
			if (!GlobalFilters.Filters.Where<Filter>(x => x.Instance is LastVisitedFilterAttribute).Any()) {
			    // Nope, let's add it then!
			    GlobalFilters.Filters.Add(new LastVisitedFilterAttribute());
			}
		}

		public void ValidateRequirements(IList<ApplicationRequirement> feedback) {
			// TODO: Anything to put here??
		}
	}
}